package view
{
	import data.Map;
	
	import element.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
    import flash.utils.*;
    import com.greensock.*;

	public class GameView extends MovieClip
	{
		private var _roles:Array = null;
		private var _elemArr:Array = null;
        
		public function GameView()
		{
            _roles = new Array();
            _elemArr = new Array();
			
			for (var i:int = 0; i < Map.row; i++) 
			{
				for (var j:int = 0; j < Map.col; j++) 
				{
					addFloor(i, j);
					insertElem(i, j, Map.data[i][j]);
				}
			}
		}
		/**
		 * 添加地图中的地板格子
		 * @param row
		 * @param col
		 * 
		 */		
		private function addFloor(row:int, col:int):void
		{
			var elem:Sprite;

			elem = new Elem(new FloorCls(), row, col);

			elem.x = col * Map.GAP;
			elem.y = row * Map.GAP;
			
			addChildAt(elem, 0);
		}
		/**
		 * 添加各个元素完善地图信息。
		 * @param row
		 * @param col
		 * @param type
		 * 
		 */		
		private function insertElem(row:int, col:int, type:int):void
		{
			var elem:Elem;
			
			if (type == Map.E_Floor) {
                _elemArr[row * Map.col + col] = null;
				return;
			} else if (type == Map.E_GrassHinder) {
				elem = new Elem(new GrassHinderCls(), row, col);
			} else if (type == Map.E_BoxHinder) {
				elem = new Elem(new BoxHinderCls(), row, col);
			} else if (type == Map.E_StoneHinder) {
				elem = new Elem(new StoneHinderCls(), row, col);
			} else if (type == Map.E_YellowHouseHinder) {
				elem = new Elem(new YellowHouseHinderCls(), row, col);
			} else if (type == Map.E_BlueHouseHinder) {
				elem = new Elem(new BlueHouseHinderCls(), row, col);
			} else if (type == Map.E_RedHouseHinder) {
				elem = new Elem(new RedHouseHinderCls(), row, col);
			} else if (type == Map.E_YellowWoodHinder) {
				elem = new Elem(new YellowWoodHinderCls(), row, col);
			} else if (type == Map.E_RedWoodHinder) {
				elem = new Elem(new RedWoodHinderCls(), row, col);
			} else if (type == Map.E_TreeHinder) {
				elem = new Elem(new TreeHinderCls(), row, col);
			} else {
                _elemArr[row * Map.col + col] = null;
				return;
			}

			elem.x = col * Map.GAP;
			elem.y = row * Map.GAP;
			addChild(elem);
            _elemArr[row * Map.col + col] = elem;
			return;
		}

        public function moveElem(row:int, col:int, elem:int, newrow:int, newcol:int, newelem:int):void
        {
            var e:Elem; 
            var idx: int;
            var newidx: int;
            
            idx = row * Map.col + col;
            if (_elemArr[idx] == null)
                return;

            e = _elemArr[idx];
            
            _elemArr[idx] = null;

            newidx = newrow * Map.col + newcol;
            if (_elemArr[idx] != null) {
                _elemArr[idx] = e;
                return;
            }

            _elemArr[newidx] = e;
            TweenLite.to(e, 2.5, {x:newcol * Map.GAP, y:newrow * Map.GAP});
            Map.data[row][col] = newelem;
            Map.data[newrow][newcol] = elem;
            return;
        }

        private function bombPaopao(r:int, c:int, owner:Person):void
        {
            var idx:int = r * Map.col + c;
            var e:Elem = new Elem(new PaopaoMovieCls, r, c);
            
            e.x = c * Map.GAP;
            e.y = r * Map.GAP;
            
            if (_elemArr[idx] == null) {
                addChildAt(e, getChildIndex(owner) - 1);
            } else {
                e.alpha = 0.45;
                addChildAt(e, getChildIndex(_elemArr[idx]) + 1);
            }
            e.gotoAndStop("bomb");
            setTimeout(removeChild, 2000, e);
        }

        private function bombPerson(r:int, c:int):void
        {
            var p:Person;

            for (var i:int=0; i<_roles.length; i++) {
                p = _roles[i];

                if(p.row == r && p.col == c) {
                    p.gotoFrame("death");
                    setTimeout(removeChild, 2000, p);
                    _roles.splice(i, 1);
                }
            }
        }

        private function bombElement(r:int, c:int):void
        {
            var idx:int = r * Map.col + c;
            var e:Elem = _elemArr[idx];

            if (Map.data[r][c] == Map.E_BoxHinder
                || Map.data[r][c] == Map.E_GrassHinder) {
                e.gotoAndPlay(2);
                setTimeout(removeChild, 500, e);
                Map.data[r][c] = Map.E_Floor;
                _elemArr[idx] = null;
            } else if (Map.data[r][c] == Map.E_Paopao) {
                var p:Paopao = e as Paopao;
                p.bomb();
            }
        }

        private function displayPaopao(p:Paopao, owner:Person):void
        {
            p.x = p.col * Map.GAP;
            p.y = p.row * Map.GAP;
            addChildAt(p, getChildIndex(owner) - 1);
        }

        private function removePaopao(p:Paopao):void
        {
            Map.data[p.row][p.col] = Map.E_Floor;
            _elemArr[p.row * Map.col + p.col] = null;
        }

        private function addPaopao(owner:Person):void
        {
            var p:Paopao;

            p = new Paopao(new PaopaoMovieCls(), owner.row, owner.col);

            p._owner = owner;
            p._bombPaopao = bombPaopao;
            p._bombPerson = bombPerson;
            p._bombElement = bombElement;
            p._displayPaopao = displayPaopao;
            p._removePaopao = removePaopao;
            
            // 占位！这个格子不能再放泡泡了
            Map.data[p.row][p.col] = Map.E_Paopao;
            _elemArr[p.row * Map.col + p.col] = p;
        }
		
        public function addPerson(row:int, col:int, role:Person):void
		{
			role.x = col * Map.GAP;
			role.y = row * Map.GAP;
            role._moveElem = moveElem;
            role._addPaopao = addPaopao;

            _roles.push(role);
			addChild(role);
		}


	}
}
